package com.yiqi.liebang.feature.home.view;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseFragment;
import com.suozhang.framework.utils.T;
import com.yiqi.liebang.R;
import com.yiqi.liebang.common.widget.HomeDialog;
import com.yiqi.liebang.entity.bo.HomeBo;
import com.yiqi.liebang.feature.home.contract.HomeContract;
import com.yiqi.liebang.feature.home.dependencies.home.DaggerHomeComponent;
import com.yiqi.liebang.feature.home.dependencies.home.HomePresenterModule;
import com.yiqi.liebang.feature.home.view.adapter.HomeAdapter;
import com.yiqi.liebang.feature.home.view.adapter.MultipleItem;
import com.yiqi.liebang.feature.login.view.LoginActivity;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends BaseFragment implements HomeContract.View, OnRefreshListener, View.OnClickListener {

    @Inject
    HomeContract.Presenter mPresenter;
    @BindView(R.id.rv_home)
    RecyclerView mRvHome;
    @BindView(R.id.smartRefesh)
    SmartRefreshLayout mSmartRefesh;
    Unbinder unbinder;
    private HomeAdapter mHomeAdapter;

    private View errorView;

    @Override
    public int attachLayoutRes() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initInjector() {
        DaggerHomeComponent.builder().homePresenterModule(new HomePresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        errorView = getLayoutInflater().inflate(R.layout.view_error, (ViewGroup) mRvHome.getParent(), false);
        errorView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData();
            }
        });
        mRvHome.setLayoutManager(new LinearLayoutManager(getActivity()));
        mSmartRefesh.setEnableLoadMore(false);
    }

    @Override
    public void onStart() {
        super.onStart();

    }

    @Override
    protected void initData() {
        mPresenter.getHomeData();

    }

    @Override
    protected void initEvent() {
        mSmartRefesh.setOnRefreshListener(this);

    }

    private void initHomeList(HomeBo homeBo) {
        mHomeAdapter = new HomeAdapter(getData(), homeBo, getActivity());
        mRvHome.setAdapter(mHomeAdapter);


    }

    @Override
    public void showSuccess(HomeBo homeBo) {
        mSmartRefesh.finishRefresh();
        initHomeList(homeBo);
    }

    @Override
    public void showError(String ex) {
//        mHomeAdapter = new HomeAdapter(null,null,getActivity());
//        mSmartRefesh.finishRefresh();
//        mHomeAdapter.setNewData(null);
//        mHomeAdapter.setEmptyView(errorView);
        T.showShort(ex);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        initData();
    }

    @Override
    public void onClick(View v) {
        initData();
    }

    private List<MultipleItem> getData() {
        List<MultipleItem> list = new ArrayList<>();
        list.add(new MultipleItem(MultipleItem.ITEM_BANNER, MultipleItem.IMG_SPAN_SIZE));
        list.add(new MultipleItem(MultipleItem.ITEM_MENU, MultipleItem.IMG_SPAN_SIZE, "奥什市哦"));
        list.add(new MultipleItem(MultipleItem.ITEM_QUESTION, MultipleItem.IMG_SPAN_SIZE));
        list.add(new MultipleItem(MultipleItem.ITEM_TOPIC, MultipleItem.IMG_SPAN_SIZE));
        return list;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.btn_home_search, R.id.btn_home_edit})
    public void onViewClicked(View view) {
        if (!AM.user().isLogin()) {
            startActivity(new Intent(getActivity(), LoginActivity.class));
            return;
        }
        switch (view.getId()) {
            case R.id.btn_home_search:
                startActivity(new Intent(AM.app(), SearchActivity.class));
                break;
            case R.id.btn_home_edit:
                HomeDialog homeDialog = new HomeDialog(getActivity());
                homeDialog.show();
                break;
        }
    }
}
